package com.share.web.action.oa.org;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.share.bean.oa.org.Department;
import com.share.bean.oa.org.Organization;
import com.share.commons.base.query.PageView;
import com.share.commons.util.DateUtil;
import com.share.commons.util.EncodingUtil;
import com.share.service.oa.org.DepartmentManager;
import com.share.service.oa.org.OrganizationManager;
import com.share.web.action.base.BaseActionSupport;
import com.share.web.formbean.oa.hr.DepartmentForm;

@Controller
@Scope("prototype")
public class DepartmentAction extends BaseActionSupport{

	private static final long serialVersionUID = 1L;

	private DepartmentForm deptform ;
	
	
	private Department dept ;
	
	@Resource
	private PageView<Department> pageView ;
	
	@Resource
	private DepartmentManager deptMgr ;
	
	@Resource
	private OrganizationManager orgMgr ;
	
	public String addDept() {
		
		try {
			this.deptMgr.saveDeptManager(dept) ;
			return SUCCESS ;
		} catch (Exception e) {
			e.printStackTrace() ;
			this.getBaseform().getRequest().setAttribute("errorsMsg","【添加部门出错】") ;
			return "errors" ;
		}
	}
	
	public String getOrgAll() {
		try {
			List<Organization> orgList = this.orgMgr.getOrgAll() ;
			
			if(null != orgList && !orgList.isEmpty()) {
				this.getBaseform().getRequest().setAttribute("orgList",orgList) ;
				
				if("searchByOrg".equals(this.getBaseform().getType())) {
					return "searchByOrg" ;
				} else {
					return "addDept" ;
				}
				
			} else {
				this.getBaseform().getRequest().setAttribute("cueMsg","【未查找到组织机构信息，请先添加】") ;
				this.getBaseform().getRequest().setAttribute("redirectUrl",this.getBaseform().getPath()+"/siteManager/oa/hr/addOrganization.jsp") ;
				return "cueMsg" ;
			}
		} catch (Exception e) {
			e.printStackTrace() ;
			this.saveMessage("添加部门时经过查找所有机构错误") ;
			this.log.info("添加部门时经过查找所有机构错误，错误原因：" + e.getMessage()) ;
			return "messages" ;
		}
	}
	
	public String deleteDept() {
		try {
			this.deptMgr.deleteDeptManager(dept.getDeptNo()) ;
			return SUCCESS ;
		} catch (Exception e) {
			e.printStackTrace() ;
			this.saveMessage("删除部门失败，该部门有员工存在") ;
			this.log.info("删除部门失败，错误原因：" + e.getMessage()) ;
			return "messages" ;
		}
	}
	
	public String updateDept() {
		try {
			this.deptMgr.updateDeptManager(dept) ;
			return SUCCESS ;
		} catch (Exception e) {
			e.printStackTrace() ;
			this.saveMessage("添加部门出错，该部门有员工存在") ;
			this.log.info("添加部门失败，错误原因：" + e.getMessage()) ;
			return "messages" ;
		}
	}
	
	public String loadByDept() {
		
		try {
			this.getBaseform().getRequest().setAttribute("dept",this.deptMgr.loadByIdDeptManager(dept.getDeptNo())) ;
			if("update".equals(this.getBaseform().getType())) {
				return "updateDept" ;
			} else {
				return "loadDept" ;
			}
		} catch (Exception e) {
			e.printStackTrace() ;
			this.getBaseform().getRequest().setAttribute("errorsMsg","【添加部门出错】") ;
			return "errors" ;
		}
	}
	
	public String showScrollDept() {
		try {
			
			if(null != deptform) {
				StringBuffer urlParam = new StringBuffer() ;
				
				if(null != deptform.getStartDate() && null != deptform.getEndDate()) {
					urlParam.append("&deptform.startDate=" + DateUtil.format(deptform.getStartDate(), DateUtil.TYPE_DATE))
					.append("&deptform.endDate=" + DateUtil.format(deptform.getEndDate(), DateUtil.TYPE_DATE));
				} else if(null != deptform.getKeyword() && !deptform.getKeyword().equals("")) {
					deptform.setKeyword(EncodingUtil.encodeURI(deptform.getKeyword())) ;
					urlParam.append("&deptform.keyword=" + deptform.getKeyword()) ;
				}
				
				//将参数保存到request范围，便于分页
				this.getBaseform().getRequest().setAttribute("urlParam",urlParam.toString()) ;
				this.getBaseform().getRequest().setAttribute("deptform",deptform) ;
			}
			pageView.setPageModel(this.deptMgr.getScrollDataDeptManager(pageView.getFirstResult(), pageView.getMaxResult(), deptform)) ;
			
			this.getBaseform().getRequest().setAttribute("pageView",pageView ) ;
			return "showScrollDept" ;
		} catch (Exception e) {
			e.printStackTrace() ;
			this.getBaseform().getRequest().setAttribute("errorsMsg","【查询部门出错】") ;
			return "errors" ;
		}
	}


	public Department getDept() {
		return dept;
	}

	public void setDept(Department dept) {
		this.dept = dept;
	}

	public void setDeptMgr(DepartmentManager deptMgr) {
		this.deptMgr = deptMgr;
	}
	public DepartmentForm getDeptform() {
		return deptform;
	}

	public void setDeptform(DepartmentForm deptform) {
		this.deptform = deptform;
	}

	public PageView<Department> getPageView() {
		return pageView;
	}

	public void setPageView(PageView<Department> pageView) {
		this.pageView = pageView;
	}
	
	public void setOrgMgr(OrganizationManager orgMgr) {
		this.orgMgr = orgMgr;
	}
}
